package io;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * 使用流链接完成
 */
public class PWDemo2 {
    public static void main(String[] args) throws FileNotFoundException {
          /*
            PrintWriter->BufferedWriter->OutputStreamWriter->FileOutputStream
            PW:负责按行写出字符串
            BW:块写文本数据加速(内部有一个默认8192长度的字符数组)
            OSW:将写出的字符转字节
            FOS:将字节写入到文件中
         */
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入内容,单独输入exit退出程序");
        FileOutputStream fos=new FileOutputStream("pw.txt");
        OutputStreamWriter ows=new OutputStreamWriter(fos, StandardCharsets.UTF_8);
        BufferedWriter bw=new BufferedWriter(ows);
        PrintWriter pw=new PrintWriter(bw,true);
        while (true){
            String str=scanner.next();
            if ("exit".equalsIgnoreCase(str)){
                break;
            }
            pw.println(str);

        }
        pw.close();
    }
}
